#!/bin/bash

# The [ -t 1 ] check only works when the function is not called from
# a subshell (like in `$(...)` or `(...)`, so this hack redefines the
# function at the top level to always return false when stdout is not
# a tty.
if [ -t 1 ]; then
  is_tty() {
    true
  }
else
  is_tty() {
    false
  }
fi

if is_tty; then
    RED=$(printf '\033[31m')
    GREEN=$(printf '\033[32m')
    YELLOW=$(printf '\033[33m')
    BLUE=$(printf '\033[34m')
    BOLD=$(printf '\033[1m')
    RESET=$(printf '\033[0m')
fi


function digitaldatetime() {
    echo `date +"%Y%m%d%H%M%S"`
}

quickVim=`pwd -P`

cd $HOME

printf "${GREEN}%s${RESET}\n" "Start to install vim config"
printf "${GREEN}%s${RESET}\n" "Looking for an existing vim config..."

if [ -f ~/.vimrc ] || [ -h ~/.vimrc ]; then
    printf "${GREEN}%s${RESET}\n" "Found ~/.vimrc. Backing up to ~/.vimrc.`digitaldatetime`";
    mv ~/.vimrc ~/.vimrc.`digitaldatetime`;
fi

if [ -f ~/.tmux.conf ] || [ -h ~/.tmux.conf ]; then
    printf "${GREEN}%s${RESET}\n" "Found ~/.tmux.conf. Backing up to ~/.tmux.conf.`digitaldatetime`";
    mv ~/.tmux.conf  ~/.tmux.conf.`digitaldatetime`;
fi

if [ -f ~/.clang-format ] || [ -h ~/.clang-format ]; then
    printf "${GREEN}%s${RESET}\n" "Found ~/.clang-format. Backing up to ~/.clang-format.`digitaldatetime`";
    mv ~/.clang-format ~/.clang-format.`digitaldatetime`;
fi

if [ -f ~/cscope.sh ] || [ -h ~/cscope.sh ]; then
    printf "${GREEN}%s${RESET}\n" "Found ~/cscope.sh. Backing up to ~/cscope.sh.`digitaldatetime`";
    mv ~/cscope.sh ~/cscope.sh.`digitaldatetime`;
fi


if [ -d ~/.vim ]; then
    printf "${GREEN}%s${RESET}\n" "Found ~/.vim. Backing up to ~/.vim.`digitaldatetime`";
    mv ~/.vim ~/.vim.`digitaldatetime`;
fi

printf "${GREEN}%s${RESET}\n" "Copying .vimrc and .vim"

printf "${GREEN}%s${RESET}\n" "ln -s ${quickVim}/vim .vim"
ln -s ${quickVim}/vim .vim

printf "${GREEN}%s${RESET}\n" "ln -s ${quickVim}/vimrc .vimrc"
ln -s ${quickVim}/vimrc .vimrc

printf "${GREEN}%s${RESET}\n" "ln -s ${quickVim}/clang-format .clang-format"
ln -s ${quickVim}/clang-format .clang-format

printf "${GREEN}%s${RESET}\n" "ln -s ${quickVim}/cscope.sh cscope.sh"
ln -s ${quickVim}/cscope.sh cscope.sh
printf "${GREEN}%s${RESET}\n" ""

printf "${GREEN}%s${RESET}\n" "ln -s ${quickVim}/tmux.conf .tmux.conf"
ln -s ${quickVim}/tmux.conf .tmux.conf
printf "${GREEN}%s${RESET}\n" ""


#                   _        __             _
#    ____ _ __  __ (_)_____ / /__   _   __ (_)____ ___
#   / __ `// / / // // ___// //_/  | | / // // __ `__ \
#  / /_/ // /_/ // // /__ / ,<     | |/ // // / / / / /
#  \__, / \__,_//_/ \___//_/|_|    |___//_//_/ /_/ /_/
#    /_/

printf '%s                  _        __   %s         _           %s\n' ${RED} ${GREEN} $RESET
printf '%s   ____ _ __  __ (_)_____ / /__ %s _   __ (_)____ ___  %s\n' ${RED} ${GREEN} $RESET
printf '%s  / __ `// / / // // ___// //_/ %s| | / // // __ `__ \ %s\n' ${RED} ${GREEN} $RESET
printf '%s / /_/ // /_/ // // /__ / ,<    %s| |/ // // / / / / / %s\n' ${RED} ${GREEN} $RESET
printf '%s \__, / \__,_//_/ \___//_/|_|   %s|___//_//_/ /_/ /_/  %s\n' ${RED} ${GREEN} $RESET
printf '%s   /_/                          %s                     %s\n' ${RED} ${GREEN} $RESET
printf '\n'

printf "${GREEN}%s${RESET}\n" ""
printf "${GREEN}%s${RESET}\n" "Happy coding!"
